$(function(){
    var obj = {
        redirect: $.getQueryString('redirect'),
        type: $.getQueryString('type'),
        account: '',
        user: {},
        userTelIpt: '',
        yzm: '',
        djs: 0,
        timer: null,
        ajaxStatus: false,
        init: function () {
            var that = this;
            that.account = localStorage.getItem('myTel') || '';
            that.addEvent();
            if(that.type == 'edit') that.ajaxGetUserMsg();
        },
        addEvent: function () {
            var that = this;
            //取消
            $('#cancel').click(function () {
                history.go(-1);
            });
            //提交
            $('#submit').click(function () {
                if(that.type === 'edit') that.ajaxEditRegister();
                else that.ajaxSubmitRegister();
            });
            //验证码
            $('.btn-yzm').click(function () {
                that.userTelIpt = $('input[name=phone]').val();
                if(!that.userTelIpt) $.alert('请输入手机号码');
                else if(isNaN(that.userTelIpt)) $.alert('请输入正确的手机号');
                else {
                    // 倒计时结束重发验证码
                    if(that.djs === 0){
                        that.resetDJS();
                        that.ajaxGetYZM();
                    }else{
                        // 倒计时未结束不重发
                    }
                }
            })
        },
        // 获取验证码
        ajaxGetYZM: function() {
            var that = this;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getYZM',
                type: 'get',
                contentType: 'application/json',
                data: {
                    tel:encodeURI(that.userTelIpt),
                },
                success: function (res) {
                    $.alert('验证码发送成功')
                },
                error: function (res) {
                }
            })
        },
        ajaxSubmitRegister: function () {
            var that = this;
            var userName = $('input[name=userName]').val();
            var age = $('input[name=age]').val();
            var sex = $('input[name=sex]:checked').val();
            var hy = $('select[name=hy]').val();
            var xl = $('select[name=xl]').val();
            var earn = $('input[name=earn]').val();
            var jobType = $('input[name=jobType]').val();
            var phone = $('input[name=phone]').val();
            var yzm = $('input[name=yzm]').val();
            if(!userName){
                $.alert('请输入姓名');
                return
            }
            if(!age){
                $.alert('请输入年龄');
                return
            }
            if(!xl){
                $.alert('请选择您的学历');
                return
            }
            if(!hy){
                $.alert('请选择行业');
                return
            }
            if(!earn){
                $.alert('请输入希望酬薪');
                return
            }
            if(!jobType){
                $.alert('请输入希望工种');
                return
            }
            if(!phone){
                $.alert('请输入手机号码');
                return
            }
            if(!yzm){
                $.alert('请输入验证码');
                return
            }
            var postData = {
                userName: userName,
                age: age,
                sex: sex,
                xl: xl,
                hy: hy,
                earn: earn,
                jobType: jobType,
                phone: phone,
                yzm: yzm
            };
            if(that.ajaxStatus) return;
            that.ajaxStatus = true;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getSubmitRegister',
                type: 'post',
                contentType: 'application/json',
                data: postData,
                success: function (res) {
                    that.ajaxStatus = false;
                    clearInterval(that.timer);
                    localStorage.setItem('myTel',phone);
                    $.alert('恭喜您，注册成功',1000,function () {
                        if(that.redirect) window.location.replace('./jobSearchList.html');
                        else history.go(-1);
                    });
                },
                error: function (res) {
                }
            })
        },
        ajaxEditRegister: function () {
            var that = this;
            var userName = $('input[name=userName]').val();
            var age = $('input[name=age]').val();
            var sex = $('input[name=sex]:checked').val();
            var hy = $('select[name=hy]').val();
            var xl = $('select[name=xl]').val();
            var earn = $('input[name=earn]').val();
            var jobType = $('input[name=jobType]').val();
            if(!userName){
                $.alert('请输入姓名');
                return
            }
            if(!age){
                $.alert('请输入年龄');
                return
            }
            if(!xl){
                $.alert('请选择您的学历');
                return
            }
            if(!hy){
                $.alert('请选择行业');
                return
            }
            if(!earn){
                $.alert('请输入希望酬薪');
                return
            }
            if(!jobType){
                $.alert('请输入希望工种');
                return
            }
            var postData = {
                userName: userName,
                age: age,
                sex: sex,
                xl: xl,
                hy: hy,
                earn: earn,
                jobType: jobType,
                phone: that.account,
            };
            if(that.ajaxStatus) return;
            that.ajaxStatus = true;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getEditRegister',
                type: 'post',
                contentType: 'application/json',
                data: postData,
                success: function (res) {
                    that.ajaxStatus = false;
                    clearInterval(that.timer);
                    localStorage.setItem('myTel',phone);
                    $.alert('恭喜您，修改成功',1000,function () {
                        if(that.redirect) window.location.replace('./jobSearchList.html');
                        else history.go(-1);
                    });
                },
                error: function (res) {
                }
            })
        },
        // 重置倒计时
        resetDJS:function () {
            var that = this;
            clearInterval(that.timer);
            that.djs = 30;
            $('.btn-yzm').html(that.djs+'s后重试');
            that.timer = setInterval(function () {
                if(that.djs>0){
                    that.djs --;
                    $('.btn-yzm').html(that.djs+'s后重试');
                }else{
                    $('.btn-yzm').html('重发验证码');
                    clearInterval(that.timer);
                }
            },1000);
        },
        // 获取用户信息
        ajaxGetUserMsg: function() {
            var that = this;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getUserMsg',
                type: 'get',
                contentType: 'application/json',
                data: {
                    account: encodeURI(that.account),
                },
                success: function (res) {
                    that.user = res.data;
                    $('.title').html('修改我的信息');
                    $('input[name=userName]').val(that.user.userName);
                    $('input[name=age]').val(that.user.age);
                    $('input[name=sex][value='+that.user.sex+']').attr('checked',true);
                    $('select[name=hy]').val(that.user.hy);
                    $('select[name=xl]').val(that.user.xl);
                    $('input[name=earn]').val(that.user.earn);
                    $('input[name=jobType]').val(that.user.jobType);
                    $('input[name=phone]').val(that.user.phone);
                    $('input[name=phone]').attr('disabled','disabled');
                    $('.yzmBox').addClass('none');
                },
                error: function (res) {
                }
            })
        }
    };
    obj.init();
});